package zh.demo.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import zh.demo.commons.R;
import zh.demo.model.VcardColumnEntity;
import zh.demo.model.VcardEntity;
import zh.demo.model.VcardVisitEntity;
import zh.demo.service.VcardColumnService;
import zh.demo.service.VcardService;
import zh.demo.service.VcardVisitService;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * Created by zhangheng on 2017/11/16.
 */
@RestController
@RequestMapping("/vcard")
public class VCardController {

    @Autowired
    private VcardService vcardService;
    @Autowired
    private VcardColumnService vcardColumnService;
    @Autowired
    private VcardVisitService vcardVisitService;

    @RequestMapping(value = "/savevcard", method = RequestMethod.POST)
    public R saveVCard(HttpServletRequest request, String jsonstr){
        String userId = request.getParameter("userId");
        String name = request.getParameter("name");
        String position = request.getParameter("position");
        String company = request.getParameter("company");
        String mobile = request.getParameter("mobile");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        String logo = request.getParameter("logo");
        String backgroundImage = request.getParameter("backgroundImage");
        String qrCode = request.getParameter("qrCode");
        VcardEntity vcard = new VcardEntity();
        vcard.setUserId(Long.valueOf(userId));
        vcard.setName(name);
        vcard.setPosition(position);
        vcard.setCompany(company);
        vcard.setMobile(mobile);
        vcard.setEmail(email);
        vcard.setAddress(address);
        vcard.setLogo(logo);
        vcard.setBackgroundImage(backgroundImage);
        vcard.setQrCode(qrCode);
        //JSONObject json = JSONObject.parseObject(jsonstr);
        //VcardEntity vcardEntity = JSONObject.parseObject(jsonstr, VcardEntity.class);
        //VcardEntity vcardEntity = json.getObject("vcard",VcardEntity.class);
        vcardService.save(vcard);
        return R.ok();
    }

    @RequestMapping(value = "/savevcardcloumn", method = RequestMethod.POST)
    public R saveVCardColumn(HttpServletRequest request, String jsonstr){
        String vcardId = request.getParameter("vcardId");
        if(StringUtils.isBlank(vcardId)){
            return R.error("名片编号不能为空");
        }
        String logo = request.getParameter("logo");
        String columnName = request.getParameter("columnName");
        String columnType = request.getParameter("columnType");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String url = request.getParameter("url");
        String author = request.getParameter("author");
        VcardColumnEntity col = new VcardColumnEntity();
        col.setVcardId(Long.valueOf(vcardId));
        col.setLogo(StringUtils.isBlank(logo)?null:logo);
        col.setColumnName(StringUtils.isBlank(columnName)?null:columnName);
        col.setColumnType(StringUtils.isBlank(columnType)?null:columnType);
        col.setTitle(StringUtils.isBlank(title)?null:title);
        col.setContent(StringUtils.isBlank(content)?null:content);
        col.setUrl(StringUtils.isBlank(url)?null:url);
        col.setAuthor(Long.valueOf(author));
        //VcardColumnEntity vcardColumnEntity = JSONObject.parseObject(jsonstr, VcardColumnEntity.class);
        vcardColumnService.save(col);
        return R.ok();
    }

    @RequestMapping(value = "/savevcardvisit", method = RequestMethod.POST)
    public R saveVCardVisit(HttpServletRequest request, String jsonstr){
        String vcardId = request.getParameter("vcardId");
        if(StringUtils.isBlank(vcardId)){
            return R.error("名片编号不能为空");
        }
        String visitType = request.getParameter("visitType");
        String name = request.getParameter("name");
        String mobile = request.getParameter("mobile");
        String project = request.getParameter("project");
        String enrollNumber = request.getParameter("enrollNumber");
        String appointmentTime = request.getParameter("appointmentTime");
        String remark = request.getParameter("remark");
        VcardVisitEntity visit = new VcardVisitEntity();
        visit.setVcardId(Long.valueOf(vcardId));
        visit.setVisitType(StringUtils.isBlank(visitType)?null:visitType);
        visit.setName(StringUtils.isBlank(name)?null:name);
        visit.setMobile(StringUtils.isBlank(mobile)?null:mobile);
        visit.setProject(StringUtils.isBlank(project)?null:project);
        visit.setEnrollNumber(StringUtils.isBlank(enrollNumber)?null:Integer.valueOf(enrollNumber));
        visit.setAppointmentTime(StringUtils.isBlank(appointmentTime)?null:new Date(appointmentTime));
        visit.setMark(StringUtils.isBlank(remark)?null:remark);
        //VcardVisitEntity vcardVisitEntity = JSONObject.parseObject(jsonstr, VcardVisitEntity.class);
        vcardVisitService.save(visit);
        return R.ok();
    }

    @RequestMapping(value = "/selectvcard", method = RequestMethod.POST)
    public R selectVCard(Long id, Long uid){
        Map<String, Object> res = vcardService.selectVcard(id,uid);
        return R.ok().put("result",res);
    }

    @RequestMapping(value = "/updatevcard", method = RequestMethod.POST)
    public R updateVCard(HttpServletRequest request, String jsonstr){
        String id = request.getParameter("id");
        String userId = request.getParameter("userId");
        String name = request.getParameter("name");
        String position = request.getParameter("position");
        String company = request.getParameter("company");
        String mobile = request.getParameter("mobile");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        String logo = request.getParameter("logo");
        String backgroundImage = request.getParameter("backgroundImage");
        String qrCode = request.getParameter("qrCode");
        VcardEntity vcard = new VcardEntity();
        vcard.setId(Long.valueOf(id));
        vcard.setUserId(Long.valueOf(userId));
        vcard.setName(name);
        vcard.setPosition(position);
        vcard.setCompany(company);
        vcard.setMobile(mobile);
        vcard.setEmail(email);
        vcard.setAddress(address);
        vcard.setLogo(logo);
        vcard.setBackgroundImage(backgroundImage);
        vcard.setQrCode(qrCode);
        //JSONObject json = JSONObject.parseObject(jsonstr);
        //VcardEntity vcardEntity = JSONObject.parseObject(jsonstr, VcardEntity.class);
        //VcardEntity vcardEntity = json.getObject("vcard",VcardEntity.class);
        vcardService.update(vcard);
        return R.ok();
    }

    @RequestMapping(value = "/updatevcardcloumn", method = RequestMethod.POST)
    public R updateVCardColumn(HttpServletRequest request, String jsonstr){
        String id = request.getParameter("id");
        String vcardId = request.getParameter("vcardId");
        if(StringUtils.isBlank(vcardId)){
            return R.error("名片编号不能为空");
        }
        String logo = request.getParameter("logo");
        String columnName = request.getParameter("columnName");
        String columnType = request.getParameter("columnType");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String url = request.getParameter("url");
        String author = request.getParameter("author");
        VcardColumnEntity col = new VcardColumnEntity();
        col.setId(Integer.valueOf(id));
        col.setVcardId(Long.valueOf(vcardId));
        col.setLogo(StringUtils.isBlank(logo)?null:logo);
        col.setColumnName(StringUtils.isBlank(columnName)?null:columnName);
        col.setColumnType(StringUtils.isBlank(columnType)?null:columnType);
        col.setTitle(StringUtils.isBlank(title)?null:title);
        col.setContent(StringUtils.isBlank(content)?null:content);
        col.setUrl(StringUtils.isBlank(url)?null:url);
        col.setAuthor(Long.valueOf(author));
        //VcardColumnEntity vcardColumnEntity = JSONObject.parseObject(jsonstr, VcardColumnEntity.class);
        vcardColumnService.update(col);
        return R.ok();
    }


}
